﻿//-----------------------------------------------------------------------
// <copyright file="ClearLogOnCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//-----------------------------------------------------------------------

namespace ChatBox.UI.Windows.Main
{
    using System.Globalization;

    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using System.Windows;

    /// <summary>
    /// Command to clear the current log on
    /// </summary>
    public class ClearLogOnCommand : GenericCommandBase<MainWindowViewModel>
    {
                /// <summary>
        /// The source of UI settings
        /// </summary>
        private readonly IServerSettingsRepository serverSettingsRepository;

        /// <summary>
        /// Initializes a new instance of the ClearLogOnCommand class
        /// </summary>
        /// <param name="serverSettingsRepository">the source of server settings</param>
        public ClearLogOnCommand(IServerSettingsRepository serverSettingsRepository)
        {
            this.serverSettingsRepository = serverSettingsRepository;
        }

        /// <summary>
        /// Execute the command with a ViewModel as parameter
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(MainWindowViewModel value)
        {
            if (value == null)
            {
                return;
            }

            ServerSettingsData serverSettingsData;

            if (value.IsLoggedOn)
            {
                serverSettingsData = this.serverSettingsRepository.ReadSettings();
                string message = string.Format(CultureInfo.CurrentUICulture, "Clearing log on details while logged on as {0} will close the chatbox. Continue?", serverSettingsData.UserName);
                MessageBoxResult result = MessageBox.Show(message, "Log off and close?", MessageBoxButton.YesNo);

                if (result != MessageBoxResult.Yes)
                {
                    return;
                }
            }

            serverSettingsData = this.serverSettingsRepository.ReadSettings();

            serverSettingsData.UserName = string.Empty;
            serverSettingsData.Password = string.Empty;

            this.serverSettingsRepository.SaveSettings(serverSettingsData);

            if (value.IsLoggedOn)
            {
                // close the app to clear login details
                value.Window.Close();
            }
        }
    }
}
